package com.abc.bt.sample.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import com.abc.bt.sample.model.User;
import com.abc.bt.sample.service.UserService;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/user")
public class UserController {

	private Logger log = Logger.getLogger(UserController.class);

	@Resource(name="userService")
	private UserService userService;
	
	/**
	 * 用户注册 
	 * http://localhost:8080/bt-gui/dispatcher/user/reg&uname=zzzz
	 * @param uname
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/reg") 
	public String reg(@RequestParam("uname")String uname, Model model) throws Exception {
		log.info("UserController.reg.handleRequest()");
		
		// test error page
		if ("1".equals(uname)) {
			throw new Exception("Test exception...");
		}
		
		// 添加新用户
		if (userService.add(uname)) {
			model.addAttribute("message", "Hello World, " + uname + "!");
		}
		
		return "index";
	}
	
	/**
	 * 用户登录
	 * http://localhost:8080/bt-gui/dispatcher/user/check?uname=zzzz&pwd=xxx
	 * @param uname
	 * @param pwd
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/check") 
	public String check(@RequestParam("uname")String uname, String pwd, Model model) {
		log.info("UserController.check.handleRequest()");
//		userService.add(uname); 
		
		// 检查用户是否存在
		if (userService.check(uname, pwd)) {
			model.addAttribute("message", "Hello World, " + uname + ", pwd: " + pwd + "!");
		} else {
			model.addAttribute("message", "Sorry, user not exist!");
			log.error("user not exist");
		}
		
		//返回页面
		return "index";
	}
	
	/**
	 * 通过@RequestParam("name")重命名uname
	 * http://localhost:8080/bt-gui/dispatcher/user?method=reg5&name=zzzz
	 * @param uname
	 * @param map
	 * @return
	 */
	@RequestMapping(params="method=reg5")
	public String reg5(@RequestParam("name")String uname,ModelMap map) { 
		log.info("UserController.req5.handleRequest()");
		log.info("uname: " + uname); 
		return "index";
	}

	/**
	 * 测试返回JSON数据
	 * @return
	 */
	@RequestMapping(value="/json")
	@ResponseBody
	public Object testJson(){
		log.info("test json............");
		int[] a = {4,34,5,53,2};
//		return sendList(Arrays.asList(a));
//		return session.getAttribute("permit");
		
//		JsonResult(true,"return ok");
		
		List<User> userList= new ArrayList<User>();
        for(int i=0; i<5; i++){
             User user= new User();
             user.setUname( "测试user"+(i+1));
             user.setPwd( "1234"+i);
             userList.add(user);
       }

        return userList;
        
	}
	
	/**
	 * test map json (no use)
	 * @param <T>
	 * @param T
	 * @return
	 */
	public static <T> Map<String, Object> sendList(List<T> T) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("root", T);
		map.put("success", true);
		return map;
	}

}
